package com.lzhpo.core.service;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.lzhpo.core.domain.PrizeInfoEntity;
import com.lzhpo.core.domain.concord.BorderDataVo;
import com.lzhpo.core.domain.concord.CodeThreeDataVo;
import com.lzhpo.core.domain.concord.SpanDataVo;
import com.lzhpo.core.domain.dragon.DragonPhoenixStaticVo;
import com.lzhpo.core.domain.dragon.DragonPhoenixVo;
import com.lzhpo.core.utils.CalculateUtil;
import com.lzhpo.core.utils.DataTypeEnum;
import com.lzhpo.core.utils.MyStrUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author <a href="lijing1@wxchina.com@wxchina.com">Lijin</a>
 * @Description TODO
 * @Date 2019/12/20 13:10
 * @Version 1.0
 **/
@Service
public class SpanDataService {

    @Autowired
    private PrizeDataService prizeDataService;


    @Autowired
    private DragonDataService dragonDataService;

    @Autowired
    private CommonService commonService;
    @Autowired
    private  CodeThreeDataService codeThreeDataService;




    private void comparaVoWithPreVo(SpanDataVo vo, SpanDataVo preVo) {
        dragonDataService.compareTwoArr(vo.getSpanValueArr(), preVo.getSpanValueArr());
        dragonDataService.compareTwoArr(vo.getMixValuesArr(), preVo.getMixValuesArr());
    }




    /**
     * @return
     */
    public List<String[]> getSpanBottomStatics(List<SpanDataVo> listResult) {

      /*  List<String[]> statics = Lists.newArrayList();
        Integer[] arrCount = new Integer[13];
        String[] arrContinue = new String[13];
        String[] arrMiss = new String[13];*/
        List<List<String>> bigList = tranSpanDataToListArr(listResult);
       return   commonService.getCommonBottomStatics(bigList,13);
        //每一行
        /*for (int i = 0; i < bigList.size(); i++) {
            List<String> temp = bigList.get(i);//21
            //每一行的各列
            for (int j = 0; j < temp.size(); j++) {
                String tempStr = temp.get(j);
                //计算出现次数
                if (tempStr.startsWith("ok")) {
                    if (arrCount[j] == null) {
                        arrCount[j] = 1;
                    } else {
                        arrCount[j] = 1 + arrCount[j];
                    }
                } else {
                    if (arrCount[j] == null) {
                        arrCount[j] = 0;
                    }

                }
                //最大遗漏
                if (tempStr.startsWith("no")) {
                    if (arrMiss[j] == null) {
                        arrMiss[j] = "1,0";
                    } else {
                        int value = Integer.valueOf(arrMiss[j].split(",")[0]) + 1;
                        int valuePre = Integer.valueOf(arrMiss[j].split(",")[1]);
                        arrMiss[j] = value + "," + valuePre;
                    }

                } else {
                    if (arrMiss[j] == null) {
                        arrMiss[j] = "0,0";
                        //断啦。将值保存
                    } else {
                        int value = Integer.valueOf(arrMiss[j].split(",")[0]);
                        int valuePre = Integer.valueOf(arrMiss[j].split(",")[1]);
                        int maxValueTemp = Math.max(value, valuePre);
                        arrMiss[j] = "0," + maxValueTemp;
                    }
                    //arrMiss[j]=0;
                }
                //最大连出 ok。  0,0   以累计的放后面，目前计算的放前面
                if (tempStr.startsWith("ok")) {

                    if (arrContinue[j] == null) {
                        arrContinue[j] = "1,0";
                    } else {
                        int value = Integer.valueOf(arrContinue[j].split(",")[0]) + 1;
                        int valuePre = Integer.valueOf(arrContinue[j].split(",")[1]);
                        arrContinue[j] = value + "," + valuePre;
                    }
                } else {
                    if (arrContinue[j] == null) {
                        arrContinue[j] = "0,0";
                        //断啦。将值保存
                    } else {
                        int value = Integer.valueOf(arrContinue[j].split(",")[0]);
                        int valuePre = Integer.valueOf(arrContinue[j].split(",")[1]);
                        int maxValueTemp = Math.max(value, valuePre);
                        arrContinue[j] = "0," + maxValueTemp;
                    }
                }
            }
        }
        List<Integer> arrContinueIntList =
                Lists.newArrayList(arrContinue).stream()
                        .map(s -> Math.max(
                                Integer.valueOf(s.split(",")[0]), Integer.valueOf(s.split(",")[1])
                        )).collect(Collectors.toList());

        List<Integer> arrMissIntList =
                Lists.newArrayList(arrMiss).stream()
                        .map(s -> Math.max(
                                Integer.valueOf(s.split(",")[0]), Integer.valueOf(s.split(",")[1])
                        )).collect(Collectors.toList());

        Integer[] arrContinueIntArr = transListToArr(arrContinueIntList);
        Integer[] arrMissIntArr = transListToArr(arrMissIntList);
        String[] arrCountStr = new String[arrCount.length];
        for (int i = 0; i < arrCountStr.length; i++) {
            arrCountStr[i] = String.valueOf(arrCount[i]);
        }
        arrCountStr = StringUtils.addStringToArray(arrCountStr, "出现次数");
        statics.add(arrCountStr);//累计
        String[] arrContinueIntArrStr = new String[arrContinueIntArr.length];
        for (int i = 0; i < arrContinueIntArr.length; i++) {
            arrContinueIntArrStr[i] = String.valueOf(arrContinueIntArr[i]);
        }
        arrContinueIntArrStr = StringUtils.addStringToArray(arrContinueIntArrStr, "最大连出");
        statics.add(arrContinueIntArrStr);//最大连出
        String[] arrMissStr = new String[arrMissIntArr.length];
        for (int i = 0; i < arrMissIntArr.length; i++) {
            arrMissStr[i] = String.valueOf(arrMissIntArr[i]);
        }
        arrMissStr = StringUtils.addStringToArray(arrMissStr, "最大遗漏");
        statics.add(arrMissStr);//最大遗漏
        return statics;*/
    }

    /**
     * 将集合转为数组
     *
     * @param list
     * @return
     */
    private Integer[] transListToArr(List<Integer> list) {

        Integer[] arr = new Integer[list.size()];
        for (int i = 0; i < list.size(); i++) {
            arr[i] = list.get(i);
        }
        return arr;
    }


    private List<List<String>> tranSpanDataToListArr(List<SpanDataVo> listResult) {
        List<List<String>> bigList = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(listResult)) {
            listResult.forEach(s -> {
                List<String> span = Lists.newArrayList(s.getSpanValueArr());
                List<String> mix = Lists.newArrayList(s.getMixValuesArr());
                List<String> listArr = Lists.newArrayList();
                listArr.addAll(span);
                listArr.addAll(mix);
                bigList.add(listArr);
            });
        }
        return bigList;
    }

    public List<SpanDataVo> getSpanIndexList() {

        List<PrizeInfoEntity> remoteList =
                prizeDataService.queryFiveHundredPrizeDataFromDb(DataTypeEnum.BEIJING_BOAT.getType());
        List<SpanDataVo> result = Lists.newArrayList();
        for (int i = 0; i < remoteList.size(); i++) {
            PrizeInfoEntity entity = remoteList.get(i);
            SpanDataVo vo = new SpanDataVo();
            commonService.copyCommonVo(entity, vo);
            int sum = CalculateUtil.getSpanValue(entity);
            String[] spanArr = MyStrUtil.getInitSumValueArr(8);
            spanArr[sum - 2] = "ok," + sum;
            vo.setSpanValueArr(spanArr);
            if (result.size() == 0) {
                //第一行默认都是断
                String[] arrTemp2 = {"no,1", "ok,断", "no,1", "ok,断", "no,1"};
                vo.setMixValuesArr(arrTemp2);
            } else {
                SpanDataVo preVo = result.get(result.size() - 1);
                int preSum = CalculateUtil.getSpanValue(preVo);
                vo.setMixValuesArr(dragonDataService.getPassBreakFallArr(sum, preSum));
                comparaVoWithPreVo(vo, preVo);
            }
            result.add(vo);

        }
        return result;
    }


}
